package main

import (
	"aydenmq/amq"
	"fmt"
	"time"
)

func main() {
	// 1. 新建一个发布者
	publisher := amq.NewPublisher("发布者", 10, 500*time.Millisecond)

	// 2. 新建3个订阅者，订阅该发布者的某主题
	happy1 := amq.NewSubscriber("订阅者h01", publisher, "快乐星球")
	happy2 := amq.NewSubscriber("订阅者h02", publisher, "快乐星球")
	sad := amq.NewSubscriber("订阅者s02", publisher, "伤心星球")

	// 3. 接收订阅，自定义行为
	go happy1.GetMsg(func(msg interface{}) {
		fmt.Printf("%s-收到信息(方式1)：%v\n", happy1.Name, msg)
	})
	go happy2.GetMsg(func(msg interface{}) {
		fmt.Printf("%s-收到信息(方式2)：%v\n", happy1.Name, msg)
	})
	go sad.GetMsg(func(msg interface{}) {
		fmt.Printf("%s-收到信息：%v\n", happy2.Name, msg)
	})

	// 4. 接收消息
	for true {
		publisher.SendMsg("快乐星球", "happy")
		publisher.SendMsg("伤心星球", "sad")
		<-time.After(5 * time.Second)
	}
}
